package cn.myframe.controller;

import cn.myframe.dto.RdStockDto;
import cn.myframe.redis.RedisService;
import cn.myframe.task.RealTimeTrading;
import cn.myframe.task.UpdateStockList;
import cn.myframe.ws.WebSocketServer;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author: ynz
 * @Date: 2019/3/19/019 17:42
 * @Version 1.0
 */
@RestController
public class Controller {

    @Autowired
    UpdateStockList updateStockList;


    @Autowired
    RealTimeTrading realTimeTrading;

    @Autowired
    RedisService redisService;

    private final static String RD_PRICE = "RD:PRICE";

    private final static String RD_ENTRUST = "RD:ENTRUST";

    private final static String RD_VOLUME = "RD:VOLUME";

    @Autowired
    WebSocketServer webSocketServer;

    @RequestMapping("/send/{code}")
    public String send(@PathVariable String code){
        String url = " http://image.sinajs.cn/newchart/min/n/Code.gif";
        RdStockDto rdStockDto = new RdStockDto();
        url = url.replaceAll("Code",code);
        rdStockDto.setStockCode(code).setUrl(url);
        webSocketServer.sendInfo(JSON.toJSONString(rdStockDto));
        return "send";
    }

    @RequestMapping("/test")
    public String test(){
        updateStockList.down();
        return "test";
    }

    @RequestMapping("/test2")
    public String test2(){
        realTimeTrading.trading();
        return "test2";
    }

    @RequestMapping("/rdStock")
    public String rdStock(){
        Set<Object> price = redisService.setMembers(RD_PRICE);
        Set<Object> entrust =redisService.setMembers(RD_ENTRUST);
        Set<Object> volume = redisService.setMembers(RD_VOLUME);

        Map<String,Set> map = new HashMap<>();
        map.put("price",price);
        map.put("entrust",entrust);
        map.put("volume",volume);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/clean")
    public String clean(){
        redisService.remove(RD_PRICE);
        redisService.remove(RD_ENTRUST);
        redisService.remove(RD_VOLUME);
        return "success";
    }
}
